package com.whj.homework.service;

import com.whj.homework.login.FailLogin;
import com.whj.homework.login.LoginType;
import com.whj.homework.login.SuccessLogin;
import com.whj.homework.mapper.UserMapper;
import com.whj.homework.pojo.User;
import com.whj.util.ServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    public LoginType login(User user, HttpSession session){
        System.out.println(user);
        FailLogin failLogin = new FailLogin();
        failLogin.setMessage("登录失败");
        failLogin.setStatus("fail");
        String username = user.getUsername();
        String password = user.getPassword();
        User u = this.userMapper.findByUserName(username);
        if(u==null){
            return failLogin;
        }
        u = this.userMapper.findByPassword(password);
        if(u==null){
            return failLogin;
        }
        SuccessLogin successLogin = new SuccessLogin();
        successLogin.setStatus("ok");
        successLogin.setMessage("登陆成功");
        session.setAttribute("username",username + System.currentTimeMillis());
        return successLogin;
    }
}
